from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import os
import time


class TestCase:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://sahitest.com/demo/clicks.htm')
        time.sleep(1)

    def click(self):  # 点击
        ac = ActionChains(self.driver)
        ipt = self.driver.find_element_by_xpath('//input[@value="click me"]')
        ac.click(ipt).perform()

    def context_click(self):  # 右击
        ac = ActionChains(self.driver)
        ipt = self.driver.find_element_by_xpath('//input[@value="right click me"]')
        ac.context_click(ipt).perform()

    def double_click(self):  # 双击
        ac = ActionChains(self.driver)
        ipt = self.driver.find_element_by_xpath('//input[@value="dbl click me"]')
        ac.double_click(ipt).perform()

    def drag_and_drop(self):  # 拖动
        self.driver.get(
            'https://sahitest.com/demo/dragDropMooTools.htm')  # https://sahitest.com/demo/dragDropMooTools.htm
        time.sleep(1)
        ac = ActionChains(self.driver)
        source = self.driver.find_element_by_id('dragger')
        target = self.driver.find_element_by_xpath('//div[4]')
        ac.drag_and_drop(source, target).perform()

    def move_to_element(self):  # 悬停
        self.driver.get('https://sahitest.com/demo/mouseover.htm')
        time.sleep(1)
        ac = ActionChains(self.driver)
        div = self.driver.find_element_by_xpath('//div/div')
        ac.move_to_element(div).perform()

    def click_and_hold(self):
        url = os.path.join(os.path.abspath('html'), 'mouse_hold.html')
        self.driver.get(url)
        time.sleep(1)
        ac = ActionChains(self.driver)
        btn = self.driver.find_element_by_id('bth1')
        ac.click_and_hold().perform()

    def quit(self):
        time.sleep(1)
        self.driver.quit()
case = TestCase()
# case.click(
# case.context_click()
# case.double_click()

case.drag_and_drop()
# case.move_to_element()
# case.click_and_hold()

# case.quit()
